package top.fengyulou.xiaofeng.controller.admin;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.fengyulou.xiaofeng.common.Jr;
import top.fengyulou.xiaofeng.common.UserUtils;
import top.fengyulou.xiaofeng.dto.MaterialDto;
import top.fengyulou.xiaofeng.service.MaterialService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

/**
 * 后台用户管理
 */
@Controller
public class AdminMaterialController {

    @Resource
    private MaterialService materialService;

    /**
     * 列表页面
     */
    @RequestMapping("/admin/material/list")
    public String list() {
        return "admin/material/admin-material-list";
    }

    //编辑页面
    @RequestMapping("/admin/material/save")
    public String savePage(String id, Model model) {
        if (StringUtils.isNotBlank(id)) {
            MaterialDto material = materialService.getById(id);
            model.addAttribute("data", material);
        }
        return "admin/material/admin-material-save";
    }

    /**
     * 试卷列表
     */
    @RequestMapping("/admin/material/child/list")
    public String childList(MaterialDto materialDto,Model model) {
        List<MaterialDto> list = materialService.list(materialDto);
        model.addAttribute("list",list);
        return "admin/material/admin-material-list-child";
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @RequestMapping("/admin/material/ajax/save")
    @ResponseBody
    public Jr ajaxSave(HttpServletRequest request, MaterialDto materialDto) {
        materialDto.setCurrentUserId(UserUtils.getUserId(request));
        materialService.save(materialDto);
        return Jr.yesd(materialDto);
    }

    /**
     * 删除
     */
    @RequestMapping("/admin/material/ajax/delete")
    @ResponseBody
    public Jr delete(@RequestParam String[] ids) {
        materialService.delete(ids);
        return Jr.yes();
    }

    /**
     * 上传
     */
    @RequestMapping("/admin/material/ajax/upload")
    @ResponseBody
    public Jr upload(HttpServletRequest request, @RequestParam("file") MultipartFile[] files) throws Exception {
        String userId = UserUtils.getUserId(request);
        String fileSpace="C:/material/images";
        String uploadPathDB="/";
        FileOutputStream fileOutputStream = null;
        InputStream inputStream = null;
        try {
            if (files != null && files.length > 0) {

                String fileName = files[0].getOriginalFilename();
                if (StringUtils.isNotBlank(fileName)) {
                    //文件上传的最终保存路径
                    String finalPath = fileSpace + "/" + uploadPathDB + "/" + fileName;
                    //设置数据库保存的路径
                    uploadPathDB += ("/" + fileName);

                    File outFile = new File(finalPath);
                    if (outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
                        //创建父文件夹
                        outFile.getParentFile().mkdirs();
                    }

                    fileOutputStream = new FileOutputStream(outFile);
                    inputStream = files[0].getInputStream();
                    IOUtils.copy(inputStream, fileOutputStream);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (fileOutputStream!=null){
                fileOutputStream.flush();
                fileOutputStream.close();
            }
        }
        return Jr.yes();
    }
}
